#!/usr/bin/python
from datetime import date
import random, sqlite3, pygtk
pygtk.require('2.0')
import gtk

default_data_file = "/usr/local/share/pytao/tao.db"

def get_passage(data_file=default_data_file):
	id = random.randint(1, 81)
	db = sqlite3.connect(data_file)
	cur = db.cursor()
	cur.execute("SELECT title, passage FROM passages WHERE rowid = ?", (id,))
	(title, passage) = cur.fetchone()
	#(title, passage) = (cur[0][0], cur[0][1])
	cur.close()
	db.close()
	return (title, passage)

class PyTaoWindow:
	def new_passage(self):
		(title, passage) = get_passage()
		self.title_box.set_markup("<b>%s</b>" % title)
		self.passage_box.set_text(passage)

	def new_button_click(self, widget):
		self.new_passage()

	def close_button_click(self, widget):
		gtk.main_quit()
	
	def delete(self, widget, event, data=None):
		gtk.main_quit()
		return False

	def __init__(self):
		self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
		self.window.set_title("PyTao - %s" % 
				date.today().strftime("%B %d, %Y"))
		self.window.connect("delete_event", self.delete)
		self.window.set_border_width(10)

		self.vert_pack = gtk.VBox(False, 0)

		self.horz_pack = gtk.HBox(False, 0)
		self.new_button = gtk.Button("New Passage")
		self.new_button.connect("clicked", self.new_button_click)
		self.horz_pack.pack_start(self.new_button)
		self.new_button.show()
		self.close_button = gtk.Button("Close")
		self.close_button.connect("clicked", self.close_button_click)
		self.horz_pack.pack_start(self.close_button)
		self.close_button.show()

		self.vert_pack.pack_end(self.horz_pack)
		self.horz_pack.show()

		self.passage_box = gtk.Label()
		self.vert_pack.pack_end(self.passage_box)
		self.passage_box.show()

		self.title_box = gtk.Label()
		self.vert_pack.pack_end(self.title_box)
		self.title_box.show()

		self.window.add(self.vert_pack)
		self.vert_pack.show()
		self.window.show()
		self.new_passage()

if __name__ == '__main__':
	pyTao = PyTaoWindow()
	gtk.main()
